package com.example.lab2.service.impl;

import com.example.lab2.api.dto.EmployeeDTO;
import com.example.lab2.domain.entity.Employee;
import com.example.lab2.domain.entity.Job;
import com.example.lab2.domain.repository.EmployeeRepository;
import com.example.lab2.infra.feign.JobFeign;
import com.example.lab2.service.EmployeeService;
import com.example.lab2.service.assembler.EmployeeAssembler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class EmployeeServiceImpl implements EmployeeService {

    private final EmployeeRepository employeeRepository;
    private final JobFeign jobFeign;

    public EmployeeServiceImpl(EmployeeRepository employeeRepository, JobFeign jobFeign) {
        this.employeeRepository = employeeRepository;
        this.jobFeign = jobFeign;
    }

    @Override
    public List<Employee> listEmployees(EmployeeDTO employeeDTO) {

        List<Employee> employeeList = employeeRepository.findEmployeeByCondition(EmployeeAssembler.toEmployee(employeeDTO));
        return employeeList;
    }

    @Override
    public Employee findEmployeeById(Integer employeeId) {
        Employee employee = employeeRepository.findEmployeeById(employeeId);
        if (null != employee) {
            Job job = jobFeign.getJobInfoById(employee.getJobId());
            log.info("job information is: {}", job);
            employee.setJob(job);
        }
        return employee;
    }

    @Override
    public Employee getEmployeeWithLeader(EmployeeDTO employeeDTO) {
        return employeeRepository.getEmployeeWithLeader(employeeDTO);
    }

    @Override
    public List<Employee> getEmployeeList(EmployeeDTO employeeDTO) {
        return employeeRepository.getEmployeeList(employeeDTO);
    }

    @Override
    public void updateEmployee(EmployeeDTO employeeDTO) {
        employeeRepository.updateEmployee(employeeDTO);
    }

    @Override
    public void insertEmployee(EmployeeDTO employeeDTO) {
        employeeRepository.insertEmployee(employeeDTO);
    }
}
